<?php


namespace app\api\controller;

use app\api\services\QCloudSmsService;
use app\data\controller\api\Login as Base;
use app\data\model\DataQiyeRzinfo;
use app\data\model\DataUser;
use app\data\service\UserAdminService;
use think\facade\Db;

class Login extends Base
{

    // public $notNeedLogin = ['getTuijianList', 'allYanchuList'];
    /**
     * PC手机登录接口
     * @throws \think\admin\Exception
     * @throws \think\db\exception\DbException
     */
    //服务员登陆
    public function in()
    {
        $data = $this->_vali([
            'phone.mobile'     => '手机号码格式错误！',
            'phone.require'    => '手机号码不能为空！',
            'password.require' => '登录密码不能为空！',
//             'verify.require'   => '验证码不能为空！',
        ]);
//        $cacheCode = $this->app->cache->get('sms_code_' . $data['phone']);
//        if (empty($cacheCode)) $this->error('验证码已过期！');
//         if ($data['verify'] !== $cacheCode) {
//             $this->error('验证码错误！');
//         }
        $this->setWebType();
        $map  = ['phone' => $data['phone'], 'deleted' => 0];
        $user = DataUser::mk()->where($map)->find();
//        if ($user->isEmpty()) {
//            $data['password'] = md5($data['password']);
//            $data['nickname'] = generateRandomUsername(8);
//            $user             = UserAdminService::set($map, $data, 'web', true);
//            //查询是否为企业用户  是企业用户默认为1
//            $user['is_enterprise_user'] = 0;
//            if (DataQiyeRzinfo::query()->where('sbr_phone', $data['phone'])->where('status', 1)
//                ->where('deleted', 0)->find()) {
//                $user['is_enterprise_user'] = 1;
//            }
//            empty($user) ? $this->error('手机注册失败！') : $this->success('用户注册成功！', $user);
//        }

//
//        dump($user);
//        exit();
        if(empty($user)){
            $this->error('手机号不存在！');
        }

        if (empty($user['status'])) {
            $this->error('该用户账号状态异常！');
        }

        if (md5($data['password']) === $user['password']) {
            //查询是否为企业用户  是企业用户默认为1
            $user                       = UserAdminService::set($map, [], 'web', true);

            $this->success('登录成功！', $user);
        }else{
            $this->error('密码错误！');
        }



    }


    public function qyin()
    {
        $data = $this->_vali([
            'phone.mobile'     => '手机号码格式错误！',
            'phone.require'    => '手机号码不能为空！',
            'password.require' => '登录密码不能为空！',
            'verify.require'   => '验证码不能为空！',
        ]);
        $cacheCode = $this->app->cache->get('sms_code_' . $data['phone']);
        if (empty($cacheCode)) $this->error('验证码已过期！');
        if ($data['verify'] !== $cacheCode) {
            $this->error('验证码错误！');
        }
        $this->setWebType();
        $map  = ['phone' => $data['phone'], 'deleted' => 0];
        $user = DataUser::mk()->where($map)->findOrEmpty();
        if ($user->isEmpty()) {
            $data['password'] = md5($data['password']);
            $data['nickname'] = generateRandomUsername(8);
            $user             = UserAdminService::set($map, $data, 'web', true);
            //查询是否为企业用户  是企业用户默认为1
            $user['is_enterprise_user'] = 0;
            if (DataQiyeRzinfo::query()->where('sbr_phone', $data['phone'])->where('status', 1)
                ->where('deleted', 0)->find()) {
                $user['is_enterprise_user'] = 1;
            }
            empty($user) ? $this->error('手机注册失败！') : $this->success('用户注册成功！', $user);
        }
        if (empty($user['status'])) {
            $this->error('该用户账号状态异常！');
        }
        if (md5($data['password']) === $user['password']) {
            //查询是否为企业用户  是企业用户默认为1
            $user                       = UserAdminService::set($map, [], 'web', true);
            $user['is_enterprise_user'] = 0;
            if (DataQiyeRzinfo::query()->where('sbr_phone', $data['phone'])->where('status', 1)
                ->where('deleted', 0)->find()) {
                $user['is_enterprise_user'] = 1;
            }

            $user['is_tijiao'] = Db::name("data_qiye_rzinfo_dsh")->where('sh_status',0)->count();
            $this->success('手机登录成功！', $user);
        }
        $this->error('账号登录失败，请稍候再试！');
    }

    public function login()
    {
        $data = $this->_vali([
            'phone.mobile'     => '手机号码格式错误！',
            'phone.require'    => '手机号码不能为空！',
            'password.require' => '登录密码不能为空！',
            // 'verify.require'   => '验证码不能为空！',
        ]);
        //$cacheCode = $this->app->cache->get('sms_code_' . $data['phone']);
        //if (empty($cacheCode)) $this->error('验证码已过期！');
        // if ($data['verify'] !== ) {
        //     $this->error('验证码错误！');
        // }
        $this->setWebType();
        $map  = ['phone' => $data['phone'], 'deleted' => 0];
        $user = DataUser::mk()->where($map)->findOrEmpty();
        if ($user->isEmpty()) {
//            $data['password'] = md5($data['password']);
//            $data['nickname'] = generateRandomUsername(8);
//            $user             = UserAdminService::set($map, $data, 'web', true);
//            //查询是否为企业用户  是企业用户默认为1
//            $user['is_enterprise_user'] = 0;
//            if (DataQiyeRzinfo::query()->where('sbr_phone', $data['phone'])->where('status', 1)
//                ->where('deleted', 0)->find()) {
//                $user['is_enterprise_user'] = 1;
//            }
//            empty($user) ? $this->error('手机注册失败！') : $this->success('用户注册成功！', $user);

            $this->error('手机号未注册',[],500);

        }
        if (empty($user['status'])) {
            $this->error('该用户账号状态异常！',[],500);
        }
        if (md5($data['password']) === $user['password']) {
            //查询是否为企业用户  是企业用户默认为1
            $user                       = UserAdminService::set($map, [], 'web', true);
            $user['is_enterprise_user'] = 0;
            if (DataQiyeRzinfo::query()->where('sbr_phone', $data['phone'])->where('status', 1)
                ->where('deleted', 0)->find()) {
                $user['is_enterprise_user'] = 1;
            }

            $user['is_tijiao'] = Db::name("data_qiye_rzinfo_dsh")->where('user_id',$user['id'])->where('sh_status',0)->count();

            $this->success('手机登录成功！', $user,200);
        }else{
            $this->error('密码错误！',[],500);
        }
    }
    /**
     * 发送短信验证码
     * @throws \Exception
     */
    public function sendsms()
    {
        $phone = $this->_vali(['phone.mobile' => '手机号码格式错误！']);
        $code  = mt_rand(1000, 9999);
        $this->app->cache->set('sms_code_' . $phone['phone'], $code, 180);
        $res = QCloudSmsService::getInstance()->sendSms($phone['phone'], $code);
        
        if ($res['result'] == 0) {
            $this->success('success！', ['code' => $code]);
        } else {
            $this->app->cache->delete('sms_code_' . $phone['phone']);
            $this->error($res['errmsg'] ?? '发送失败！请稍后再试');
        }
    }
}
